packages=$(shell ls -C java | sed 's/\s\+/:/g' | sed -e :a -e '$!N; s/\n/:/; ta')
dirs=$(shell cd java ; find . -type d -iregex '..+' \! -iregex '.*\.svn.*')
srcs=$(shell cd java ; find . -iname '*.java' \! -iregex '.*\.svn.*')
res=$(shell cd java ; find . -type f \! -iname '*.java' \! -iregex '.*\.svn.*')

JNI_CPP_FLAGS = -shared -Wall -Wextra -O2
JNI_CPP_INC = jni/jni_helper.cpp
JNI_CPP_INCDIR = -I$(JAVA_ROOT)/include -I$(JAVA_ROOT)/include/linux
JAVA_BIN_DIR = $(JAVA_ROOT)/bin/

LAUNCH4J_URL = http://sourceforge.net/projects/launch4j/files/launch4j-3/3.0.1/launch4j-3.0.1-linux.tgz/download

ifeq ($(DEBUGMODE), 1)
	JAVA_BUILD_FLAGS = -Xlint:all,-serial -g
else
	JAVA_BUILD_FLAGS = -Xlint:all,-serial
endif

dist: jni-bin java-build
	mkdir dist
	cd java-build ; jar cfme ../dist/UniLANChat.jar ../manifest.mf main.Main  *
ifeq ($(DEBUGMODE), 1)
	cp launcher/unix/UniLANChat-debug dist/UniLANChat
else
	cp launcher/unix/UniLANChat dist/UniLANChat
endif
	chmod +x dist/UniLANChat
	cp launcher/windows/UniLANChat.exe dist/UniLANChat.exe

all: UniLANChat-src.tar.gz dist java-doc UniLANChat-bin.zip UniLANChat-bin.tar.gz UniLANChat-doc.tar.gz

java-build:
	mkdir java-build
	cd java-build ; \
	for dir in $(dirs) ; do \
		mkdir $$dir ; \
	done
	cd java ; \
	javac $(JAVA_BUILD_FLAGS) -d ../java-build $(srcs)
	for resi in $(res) ; do \
		cp java/$$resi java-build/$$resi ; \
	done

jni-headers: java-build
	$(JAVA_BIN_DIR)javah -d jni -classpath java-build tools.systemintegration.X11StartupNotification

jni-bin-linux32: jni-headers
	g++ -m32 $(JNI_CPP_FLAGS) -lX11 jni/tools_systemintegration_X11StartupNotification.cpp $(JNI_CPP_INC) \
		-o java/jni/libtools_systemintegration_X11StartupNotification.so $(JNI_CPP_INCDIR)
	chmod -x java/jni/libtools_systemintegration_X11StartupNotification.so

jni-bin-linux64: jni-headers
	g++ -m64 -fPIC $(JNI_CPP_FLAGS) -lX11 jni/tools_systemintegration_X11StartupNotification.cpp $(JNI_CPP_INC) \
		-o java/jni/libtools_systemintegration_X11StartupNotification-64.so $(JNI_CPP_INCDIR)
	chmod -x java/jni/libtools_systemintegration_X11StartupNotification-64.so

jni-bin: \
	java/jni/libtools_systemintegration_X11StartupNotification.so \
	java/jni/libtools_systemintegration_X11StartupNotification-64.so

launcher-rebuild:
	mkdir launcher-build
	cd launcher-build ; wget $(LAUNCH4J_URL) -O launch4j.tgz
	cd launcher-build ; tar xzf launch4j.tgz
	cd launcher-build/launch4j ; ./launch4j ../../launcher/windows/launcher.xml
	chmod -x launcher-build/UniLANChat.exe
	cp launcher-build/UniLANChat.exe launcher/windows

java-doc:
	mkdir java-doc
	javadoc -d java-doc -encoding utf8 -docencoding utf8 -charset utf8 \
		-protected -nodeprecated -nohelp -noindex -author -windowtitle UniChat \
		-tag todo:a:"TODO:" -sourcepath java -subpackages ${packages}

dist-zip: dist java-doc
	mkdir dist-zip
	mkdir dist-zip/UniLANChat-bin
	mkdir dist-zip/UniLANChat-doc
	mkdir dist-zip/UniLANChat-doc/javadoc
	cp -r dist/* dist-zip/UniLANChat-bin
	cp -r java-doc/* dist-zip/UniLANChat-doc/javadoc
	cp docs/developers-guide/unilanchat-devguide.pdf dist-zip/UniLANChat-doc
	cp docs/user-guide/unilanchat-userguide.pdf dist-zip/UniLANChat-doc

UniLANChat-bin.zip: dist-zip
	cd dist-zip ; zip -r ../UniLANChat-bin.zip UniLANChat-bin

UniLANChat-bin.tar.gz: dist-zip
	cd dist-zip ; tar --owner nobody --group nobody -czf ../UniLANChat-bin.tar.gz UniLANChat-bin

UniLANChat-doc.tar.gz: dist-zip
	cd dist-zip ; tar --owner nobody --group nobody -czf ../UniLANChat-doc.tar.gz UniLANChat-doc

UniLANChat-src.tar.gz: clean
	tar --owner nobody --group nobody --create --gzip --file=UniLANChat-src.tar.gz --exclude=Makefile --exclude=.svn *

clean:
	@rm -f -r dist
	@rm -f -r java-doc
	@rm -f -r java-build
	@rm -f -r launcher-build
	@rm -f -r dist-zip
	@rm -f UniLANChat-bin.zip
	@rm -f UniLANChat-bin.tar.gz
	@rm -f UniLANChat-doc.tar.gz
	@rm -f UniLANChat-src.tar.gz
	@rm -f jni/tools_systemintegration_X11StartupNotification.h

clean-all: clean
	@rm -f Makefile

clean-jni:
	@rm -f java/jni/*.so
	@rm -f java/jni/*.dll
